package com.b2b.controller.company;

import com.b2b.model.CompanyRouteAuthorityDto;
import com.b2b.service.CompanyApiAuthorityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.CompanyApiAuthorityDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;

import com.b2b.util.CompanyApiAuthorityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/companyApi")
@PropertySource("classpath:config.properties")
public class CompanyApiAuthorityController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final CompanyApiAuthorityUtil companyApiAuthorityUtil;
    private final CompanyApiAuthorityService companyApiAuthorityService;

    @Autowired
    public CompanyApiAuthorityController(CompanyApiAuthorityUtil companyApiAuthorityUtil,CompanyApiAuthorityService companyApiAuthorityService){
        this.companyApiAuthorityService = companyApiAuthorityService;
        this.companyApiAuthorityUtil = companyApiAuthorityUtil;
    }

    /**
     * 新增公司API权限
     * @param companyApiAuthorityDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @PostMapping("/add")
//    @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean add(@RequestBody CompanyApiAuthorityDto companyApiAuthorityDto) {
        // 判断当前名称是否存在
        CompanyApiAuthorityDto companyApiAuthorityDtoTemp = new CompanyApiAuthorityDto();
        companyApiAuthorityDtoTemp.setApiName(companyApiAuthorityDto.getApiName());
        companyApiAuthorityDtoTemp = companyApiAuthorityService.selectOne(companyApiAuthorityDtoTemp);
        if (companyApiAuthorityDtoTemp != null ) {
            throw new CustomUnauthorizedException("该API名称已存在(Account exist.)");
        }
        int count = companyApiAuthorityService.insert(companyApiAuthorityDto);
        if (count <= 0) {
            throw new CustomException("新增失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", companyApiAuthorityDto);
    }

    @GetMapping("/getTree")
    public ResponseBean findTree(){
        List<CompanyApiAuthorityDto> companyApiAuthorityDtoList = companyApiAuthorityService.selectAll();
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", companyApiAuthorityDtoList);
    }
    /**
     * 获取公司API权限
     * @param
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @GetMapping("/getAll")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean findAll(BaseDto baseDto) {
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<CompanyApiAuthorityDto> companyApiAuthorityDtos = companyApiAuthorityService.selectAll();
        PageInfo<CompanyApiAuthorityDto> selectPage = new PageInfo<CompanyApiAuthorityDto>(companyApiAuthorityDtos);
        if (companyApiAuthorityDtos == null) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    /**
     * 更新公司API权限
     * @param companyApiAuthorityDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @PutMapping("/update")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean update( @RequestBody CompanyApiAuthorityDto companyApiAuthorityDto) {
        // 查询数据库密码
        CompanyApiAuthorityDto companyApiAuthorityDtoTemp = new CompanyApiAuthorityDto();
        companyApiAuthorityDtoTemp.setApiId(companyApiAuthorityDto.getApiId());
        companyApiAuthorityDtoTemp = companyApiAuthorityService.selectOne(companyApiAuthorityDtoTemp);
        if (companyApiAuthorityDtoTemp == null) {
            throw new CustomUnauthorizedException("该帐号不存在(Account not exist.)");
        }
        int count = companyApiAuthorityService.updateByPrimaryKeySelective(companyApiAuthorityDto);

        if (count <= 0) {
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", companyApiAuthorityDto);
    }

    /**
     * 删除相关公司API权限
     * @param apiId
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:43
     */
    @DeleteMapping("/delete")
    // @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean delete(@RequestParam Integer apiId) {
        int count = companyApiAuthorityService.deleteByPrimaryKey(apiId);
        if (count <= 0) {
            throw new CustomException("删除失败，ID不存在(Deletion Failed. ID does not exist.)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }
}
